Flying Foxes




plotly_flyfox <- ggplot() + geom_point(data=data_flyfox, 
                                   aes(utm.easting, utm.northing,
                                       color=individual.local.identifier)) +
                        labs(x="Easting", y="Northing") +
                        guides(color=guide_legend("Identifier"))

plotly::ggplotly(plotly_flyfox)
data_flyfox %>% group_by(individual.local.identifier) %>%
    summarise(n())
## # A tibble: 27 × 2
##    individual.local.identifier `n()`
##                          <int> <int>
##  1                         261   284
##  2                         325    54
##  3                         423   260
##  4                         434   197
##  5                         486   131
##  6                         556   528
##  7                         570   310
##  8                         577   321
##  9                         602   219
## 10                         606   285
## # ℹ 17 more rows
list_flyfox_ind_full <- split(data_flyfox, data_flyfox$individual.local.identifier)

list_flyfox_ind <- list_flyfox_ind_full[1:10]
flyfox_ind <- names(list_flyfox_ind)


Santa Living at the North Pole is a Clever Misdirection


ortho <- "images/christmas.tif"

image_stack <- raster::stack(ortho)

crop_full <- raster::extent(min(data_flyfox$utm.easting-5000), 
                            max(data_flyfox$utm.easting+5000), 
                            min(data_flyfox$utm.northing-5000), 
                            max(data_flyfox$utm.northing+5000))


x_coord <- c(min(data_flyfox$utm.easting-5000), max(data_flyfox$utm.easting+5000))
y_coord <- c(min(data_flyfox$utm.northing-5000), max(data_flyfox$utm.northing+5000))

image_full <- crop(image_stack, crop_full)

map_christmas <- as.data.frame(image_full, xy = TRUE) %>% setNames(c("x", "y", "red", "green", "blue"))

ggplot()+geom_spatial_rgb(data = map_christmas, aes(x=x, y=y, r=red, g=green, b=blue))+coord_sf() + scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))


KDE Loop Attempt 1


function_kde <- function(individual_kde){
  data <- individual_kde
  x <- as.data.frame(data$utm.easting)
  y <- as.data.frame(data$utm.northing)
  xy <- c(x,y)
  data.proj <- SpatialPointsDataFrame(xy, data, proj4string = CRS("+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs"))
  xy.sp <- SpatialPoints(data.proj@coords)
  kde<-kernelUD(xy.sp, h="href", kern="bivnorm", extent = 3, grid=200)
  ver95 <- getverticeshr(kde, 95)
  ver85 <- getverticeshr(kde, 85)
  ver75 <- getverticeshr(kde, 75)
  kde.points <- cbind((data.frame(xy)),data$individual.local.identifier)
  colnames(kde.points) <- c("x","y","identifier")
  kde95.sf <- st_as_sf(ver95)
  kde85.sf <- st_as_sf(ver85)
  kde75.sf <- st_as_sf(ver75)
  kde.plot <- ggplot() + theme_bw() + 
    theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) + 
    geom_spatial_rgb(data = map_christmas, aes(x=x, y=y, r=red, g=green, b=blue)) +
    geom_sf(data=kde95.sf, fill = "blue", alpha = 0.4) +
    geom_sf(data=kde85.sf, fill = "green", alpha = 0.3) +
    geom_sf(data=kde75.sf, fill = "yellow", alpha = 0.2) +
    geom_point(data=kde.points, aes(x=x, y=y), color = "lightgray") + 
    coord_sf(xlim = x_coord,  ylim = y_coord) +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    labs(x="Easting (m)", y="Northing (m)", title=kde.points$identifier) +
    theme(legend.position="none", plot.title = element_text(face = "bold", hjust = 0.5))
  kde.plot + annotate("text", x = 560000, y = 8831000, label = paste("95% ",round(ver95$area,2)," ha"), 
                      fontface = "bold", size = 3, color = "white")
}

pblapply(list_flyfox_ind, function_kde)
## $`261`

## 
## $`325`

## 
## $`423`

## 
## $`434`

## 
## $`486`

## 
## $`556`

## 
## $`570`

## 
## $`577`

## 
## $`602`

## 
## $`606`


KDE Loops Adjusted


function_kde_2 <- function(individual_kde_2){
  data <- individual_kde_2
  x <- as.data.frame(data$utm.easting)
  y <- as.data.frame(data$utm.northing)
  xy <- c(x,y)
  xspec <- c(min(x-1000), max(x+1000))
  yspec <- c(min(y-1000), max(y+1000))
  xanno <- min(x-500)
  yanno <- min(y-500)
  data.proj <- SpatialPointsDataFrame(xy, data, proj4string = CRS("+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs"))
  xy.sp <- SpatialPoints(data.proj@coords)
  kde<-kernelUD(xy.sp, h="href", kern="bivnorm", extent = 3, grid=200)
  ver95 <- getverticeshr(kde, 95)
  ver85 <- getverticeshr(kde, 85)
  ver75 <- getverticeshr(kde, 75)
  kde.points <- cbind((data.frame(xy)),data$individual.local.identifier)
  colnames(kde.points) <- c("x","y","identifier")
  kde95.sf <- st_as_sf(ver95)
  kde85.sf <- st_as_sf(ver85)
  kde75.sf <- st_as_sf(ver75)
  kde.plot <- ggplot() + theme_bw() + 
    theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) + 
    geom_spatial_rgb(data = map_christmas, aes(x=x, y=y, r=red, g=green, b=blue)) +
    geom_sf(data=kde95.sf, fill = "blue", alpha = 0.4) +
    geom_sf(data=kde85.sf, fill = "green", alpha = 0.3) +
    geom_sf(data=kde75.sf, fill = "yellow", alpha = 0.2) +
    geom_point(data=kde.points, aes(x=x, y=y), color = "lightgray") + 
    coord_sf(xlim = xspec,  ylim = yspec) +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    labs(x="Easting (m)", y="Northing (m)", title=kde.points$identifier) +
    theme(legend.position="none", plot.title = element_text(face = "bold", hjust = 0.5))
  kde.plot + annotate("text", x = xanno, y = yanno, label = paste("95% ",round(ver95$area,2)," ha"), 
                      fontface = "bold", size = 4, color = "white")
}

pblapply(list_flyfox_ind, function_kde_2)
## $`261`

## 
## $`325`

## 
## $`423`

## 
## $`434`

## 
## $`486`

## 
## $`556`

## 
## $`570`

## 
## $`577`

## 
## $`602`

## 
## $`606`


MCP


function_mcp <- function(individual_mcp){
  data <- individual_mcp
  x <- as.data.frame(data$utm.easting)
  y <- as.data.frame(data$utm.northing)
  xy <- c(x,y)
  xspec <- c(min(x-1000), max(x+1000))
  yspec <- c(min(y-1000), max(y+1000))
  xanno <- min(x-500)
  yanno <- min(y-500)
  data.proj <- SpatialPointsDataFrame(xy, data, proj4string = CRS("+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs"))
  xy.sp <- SpatialPoints(data.proj@coords)
  mcp.out <- mcp(xy.sp, percent=100, unout="ha")
  mcp.sf <- st_as_sf(mcp.out)
  mcp.points <- cbind((data.frame(xy)),data$individual.local.identifier)
  colnames(mcp.points) <- c("x","y", "identifier")

mcp.plot <- ggplot() + theme_bw() +
  theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) + 
  theme(panel.grid = element_blank()) +
  geom_spatial_rgb(data = map_christmas, aes(x=x, y=y, r=red, g=green, b=blue)) +
  geom_sf(data=mcp.sf, alpha = 0.2) +
  geom_point(data=mcp.points, aes(x=x, y=y), color = "lightgreen") + 
  labs(x="Easting (m)", y="Northing (m)", title=mcp.points$identifier) +
  theme(legend.position="none", plot.title = element_text(face = "bold", hjust = 0.5)) +
  coord_sf(xlim = xspec,  ylim = yspec)
mcp.plot + annotate("text", x = xanno, y = yanno, label = paste(round(mcp.out@data$area,2),"ha"), 
                    fontface = "bold", size = 4, color = "white")
}

pblapply(list_flyfox_ind, function_mcp)
## $`261`

## 
## $`325`

## 
## $`423`

## 
## $`434`

## 
## $`486`

## 
## $`556`

## 
## $`570`

## 
## $`577`

## 
## $`602`

## 
## $`606`